package com.lxm.shownews.commons;

import android.content.Context;
import android.database.Cursor;

import com.lxm.shownews.bean.ChannelInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxm on 2016/2/1.
 */
public class DBUtils {

    private static DBOpenHelper dbOpenHelper;
    private static DBUtils dbInstance;

    private DBUtils(Context context) {
        dbOpenHelper = new DBOpenHelper(context);
    }

    public static synchronized DBUtils getDbInstance(Context context) {
        if (dbInstance == null)
            dbInstance = new DBUtils(context);
        return dbInstance;
    }

    /**
     * 插入新闻频道
     *
     * @param channelInfo
     * @return
     */
    public static boolean insertChannelInfo(ChannelInfo channelInfo) {
        int count = dbOpenHelper.selectCount("select * from channel_info where channelId = ?", new String[]{channelInfo.getChannelId()});
        if (count != 0)
            return false;
        return dbOpenHelper.updateData("insert into channel_info(channelId,name,sortNum) values(?,?,?)",
                new Object[]{channelInfo.getChannelId(), channelInfo.getName(), channelInfo.getSortNum()});//channelInfo.getSortNum(),
    }

    /**
     * 插入新闻频道列表
     *
     * @param channelInfoList
     * @return
     */
    public static boolean insertChannelInfoList(List<ChannelInfo> channelInfoList) {
        dbOpenHelper.getSQLiteDatabase().beginTransaction();
        try {
            for (ChannelInfo channelInfo : channelInfoList) {
                insertChannelInfo(channelInfo);
            }
            dbOpenHelper.getSQLiteDatabase().setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            dbOpenHelper.getSQLiteDatabase().endTransaction();
        }
        return true;
    }

    /**
     * 查询所有新闻频道
     *
     * @return
     */
    public static List<ChannelInfo> selectAllChannelInfo() {
        Cursor cursor = dbOpenHelper.selectCursor("select * from channel_info", null);
        List<ChannelInfo> channelInfos = null;
        if (cursor != null) {
            channelInfos = new ArrayList<>();
            while (cursor.moveToNext()) {
                ChannelInfo channelInfo = new ChannelInfo();
                channelInfo.setChannelId(cursor.getString(1));
                channelInfo.setName(cursor.getString(2));
                channelInfo.setSortNum(cursor.getInt(3));
                channelInfos.add(channelInfo);
            }
            cursor.close();
        }
        return channelInfos;
    }

    /**
     * @return
     */
    public static List<ChannelInfo> selectSortChannelInfos() {
        Cursor cursor = dbOpenHelper.selectCursor("select * from channel_info where sortNum >0", null);
        List<ChannelInfo> channelInfos = null;
        if (cursor != null) {
            channelInfos = new ArrayList<>();
            while (cursor.moveToNext()) {
                ChannelInfo channelInfo = new ChannelInfo();
                channelInfo.setChannelId(cursor.getString(1));
                channelInfo.setName(cursor.getString(2));
                channelInfo.setSortNum(cursor.getInt(3));
                channelInfos.add(channelInfo);
            }
            cursor.close();
        }
        return channelInfos;
    }



    public static int selectChannelInfoNum() {
        return dbOpenHelper.selectCount("select * from channel_info", null);
    }

    public static boolean updateChannelInfo(ChannelInfo channelInfo) {
        return dbOpenHelper.updateData("update channel_info set channelId=? and name=? and sortNum=?",
                new Object[]{channelInfo.getChannelId(), channelInfo.getName(), channelInfo.getSortNum()});
    }

    public static boolean updateChannelInfoList(List<ChannelInfo> channelInfoList) {
        dbOpenHelper.getSQLiteDatabase().beginTransaction();
        try {
            for (ChannelInfo channelInfo : channelInfoList) {
                updateChannelInfo(channelInfo);
            }
            dbOpenHelper.getSQLiteDatabase().setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            dbOpenHelper.getSQLiteDatabase().endTransaction();
        }
        return true;
    }
}
